package com.ruoyi.prod.controller;

import com.github.pagehelper.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.prod.entity.po.ProdPurchase;
import com.ruoyi.prod.enums.AuditState;
import com.ruoyi.prod.enums.LogModuleEnum;
import com.ruoyi.prod.enums.LogTypeEnum;
import com.ruoyi.prod.enums.SysAction;
import com.ruoyi.prod.service.BaseLogService;
import com.ruoyi.prod.service.ProdPurchaseService;
import com.ruoyi.prod.util.IsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 采购管理-采购计划 前端控制器
 * </p>
 *
 * @author 强子初
 * @since 2023-02-17
 */
@Api(tags = "采购管理-采购计划")
@RestController
@RequestMapping("/prod/purchase/plan")
public class ProdPurchasePlanController {

    @Autowired
    private ProdPurchaseService baseService;

    @Autowired
    private BaseLogService logService;

    /**
     * 详情
     */
    @ApiOperation("详情")
    @GetMapping("/{id}")
    public R<ProdPurchase> getInfo(@PathVariable String id) {
        return R.ok(baseService.getById(id));
    }


    /**
     * 列表
     */
    @ApiOperation("列表")
    @PostMapping("/list")
    public R<List<ProdPurchase>> list(@Validated @RequestBody ProdPurchase condition) {
        return R.ok(baseService.list(condition));
    }

    /**
     * 分页
     */
    @ApiOperation("分页")
    @PostMapping("/page")
    public R<List<ProdPurchase>> page(@Validated @RequestBody ProdPurchase condition) {
        Page<ProdPurchase> page = baseService.page(condition);
        return R.page(page.getResult(), page.toPageInfo());
    }

    /**
     * 新增
     */
    @ApiOperation("新增")
//    @BaseLog(module = , type = "${po.action}")
    @PostMapping("/add")
    public R<?> add(@Validated @RequestBody ProdPurchase po) {
        po.setAuditState(IsUtil.getAuditState(po.getAction()));
        po.setOdd(baseService.getOdd());
        baseService.saveOrUpdate(po);

        // TODO 日志 需要改
        LogTypeEnum logType = IsUtil.getLogType(po.getAction());
        logService.save(po.getId(), LogModuleEnum.PURCHASE, logType);

        return R.ok(null, "新增成功");
    }

    /**
     * 删除
     */
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public R<?> remove(@PathVariable String id) {
        AuditState state = baseService.getById(id).getAuditState();
        if (IsUtil.okAction(state, SysAction.DELETE))
            throw new ServiceException("当前状态不允许修改");
        baseService.removeById(id);

        // TODO 日志 需要改
        logService.save(id, LogModuleEnum.PURCHASE, LogTypeEnum.DELETE);
        return R.ok(null, "删除成功");
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @PutMapping("/edit")
    public R<?> edit(@Validated @RequestBody ProdPurchase po) {

        AuditState state = baseService.getById(po.getId()).getAuditState();
        SysAction action = po.getAction();
        if (IsUtil.okAction(state, action))
            throw new ServiceException("当前状态不允许修改");

        po.setAuditState(IsUtil.getAuditState(po.getAction()));

        baseService.saveOrUpdate(po);

        // TODO 日志 需要改
        LogTypeEnum logType = IsUtil.getLogType(po.getAction());
        logService.save(po.getId(), LogModuleEnum.PURCHASE, logType, po.getMessage());
        return R.ok(null, "修改成功");
    }
}